package com.cskaoyan.service;

import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.CategoryExample;
import com.cskaoyan.bean.category.bo.CategoryCreateBo;
import com.cskaoyan.bean.category.bo.CategoryDeleteBo;
import com.cskaoyan.bean.category.bo.CategoryUpdateBo;
import com.cskaoyan.bean.category.vo.CategoryL1Vo;
import com.cskaoyan.bean.category.vo.CategoryListVo;
import com.cskaoyan.bean.category.vo.ChildrenBean;
import com.cskaoyan.mapper.CategoryMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
/**
 * @author wangdezhao
 * @date 2021/6/5
 * @description 商品类目
 */
@Service
public class CategoryServiceImpl implements CategoryService {
@Autowired
CategoryMapper categoryMapper;

    /**
     * 输出商品类目
     * @return
     */

    @Override
    public   List<CategoryListVo> getCategoryList() {
        List<CategoryListVo> categoryListVos = new ArrayList<CategoryListVo>();
        CategoryExample categoryExample1 = new CategoryExample();
        CategoryExample categoryExample2= new CategoryExample();
        CategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
        CategoryExample.Criteria criteria2 = categoryExample2.createCriteria();

        criteria1.andLevelEqualTo("L1").andDeletedEqualTo(false);
        criteria2.andLevelEqualTo("L2").andDeletedEqualTo(false);

        List<Category> categoryFatherList = categoryMapper.selectByExample(categoryExample1);// 全部父类
        List<Category> categorySonList = categoryMapper.selectByExample(categoryExample2); // 全部子类



        for (Category father : categoryFatherList) {
            CategoryListVo categoryListVo = new CategoryListVo();
            // vo 赋值
            categoryListVo.setId(father.getId());
            categoryListVo.setName(father.getName());
            categoryListVo.setKeywords(father.getKeywords());
            categoryListVo.setDesc(father.getDesc());
            categoryListVo.setIconUrl(father.getIconUrl());
            categoryListVo.setPicUrl(father.getPicUrl());
            categoryListVo.setLevel(father.getLevel());
            List<ChildrenBean> childrenBeanList = new ArrayList<>();// childrenBean赋值
            categoryListVo.setChildren(childrenBeanList);
            for (Category son : categorySonList) {
                if(son.getPid().equals(father.getId())){
                    ChildrenBean childrenBean = new ChildrenBean();
                    childrenBean.setId(son.getId());
                    childrenBean.setName(son.getName());
                    childrenBean.setKeywords(son.getKeywords());
                    childrenBean.setDesc(son.getDesc());
                    childrenBean.setIconUrl(son.getIconUrl());
                    childrenBean.setPicUrl(son.getPicUrl());
                    childrenBean.setLevel(son.getLevel());
                    childrenBeanList.add(childrenBean);
                }
            }
            categoryListVos.add(categoryListVo);
        }
        return categoryListVos;
    }

    @Override
    public List<CategoryL1Vo> getL1List() {
        List<CategoryL1Vo> categoryL1Vos = new ArrayList<>();

        CategoryExample categoryExample1 = new CategoryExample();
        CategoryExample.Criteria criteria1 = categoryExample1.createCriteria();
        criteria1.andLevelEqualTo("L1");
        List<Category> categoryFatherList = categoryMapper.selectByExample(categoryExample1);// 全部父类

        for (Category father : categoryFatherList) {
            CategoryL1Vo categoryL1Vo = new CategoryL1Vo();
            categoryL1Vo.setValue(father.getId());
            categoryL1Vo.setLabel(father.getName());

            categoryL1Vos.add(categoryL1Vo);
        }

        return categoryL1Vos;
    }

    /**
     * 插入商品类目
     * @param createBo
     * @return
     *
     */
    @Override
    public Integer createCategory(CategoryCreateBo createBo) {
        // bo 转换成 pojo
        Category category = new Category();
        category.setPid(createBo.getPid());
        category.setName(createBo.getName());
        category.setKeywords(createBo.getKeywords());
        category.setLevel(createBo.getLevel());
        category.setPicUrl(createBo.getPicUrl());
        category.setAddTime(new Timestamp(System.currentTimeMillis()));
        // insert
//        CategoryExample categoryExample = new CategoryExample();
//        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        int effectedRows = categoryMapper.insertSelective(category);

        return effectedRows;
    }

    @Override
    public Integer updateCategory(CategoryUpdateBo updateBo) {
        // bo 转换成 pojo
        Category category = new Category();

        category.setName(updateBo.getName());
        category.setKeywords(updateBo.getKeywords());
        category.setLevel(updateBo.getLevel());
        category.setPicUrl(updateBo.getPicUrl());
        category.setIconUrl(updateBo.getIconUrl());
        category.setPicUrl(updateBo.getPicUrl());
        category.setLevel(updateBo.getLevel());
        // 设置更改时间
        category.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andIdEqualTo(updateBo.getId());
        int effectedRows = categoryMapper.updateByExampleSelective(category,categoryExample);

        return effectedRows;
    }

    @Override
    public Integer deleteCategory(CategoryDeleteBo deleteBo) {
        Integer id = deleteBo.getId();
        int effectedRows = categoryMapper.deteleCategoryById(id);
        return effectedRows;
    }
}
